﻿IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Explanations]') AND type in (N'U'))
DROP TABLE [Explanations]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [Explanations](
	[ExplanationID] [int] IDENTITY(1,2000) NOT NULL,
	[ExplanationText] [nvarchar](500) NOT NULL,
 CONSTRAINT [PK_Explanations] PRIMARY KEY CLUSTERED 
(
	[ExplanationID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

--region Drop Existing Procedures

IF OBJECT_ID(N'[dbo].[spExplanationIns]') IS NOT NULL
	DROP PROCEDURE [dbo].[spExplanationIns]

IF OBJECT_ID(N'[dbo].[spExplanationUpd]') IS NOT NULL
	DROP PROCEDURE [dbo].[spExplanationUpd]

IF OBJECT_ID(N'[dbo].[spExplanationInsUpd]') IS NOT NULL
	DROP PROCEDURE [dbo].[spExplanationInsUpd]

IF OBJECT_ID(N'[dbo].[spExplanationDel]') IS NOT NULL
	DROP PROCEDURE [dbo].[spExplanationDel]

IF OBJECT_ID(N'[dbo].[spExplanationDelDyn]') IS NOT NULL
	DROP PROCEDURE [dbo].[spExplanationDelDyn]

IF OBJECT_ID(N'[dbo].[spExplanationSel]') IS NOT NULL
	DROP PROCEDURE [dbo].[spExplanationSel]

IF OBJECT_ID(N'[dbo].[spExplanationSelDyn]') IS NOT NULL
	DROP PROCEDURE [dbo].[spExplanationSelDyn]

IF OBJECT_ID(N'[dbo].[spExplanationSelAll]') IS NOT NULL
	DROP PROCEDURE [dbo].[spExplanationSelAll]

IF OBJECT_ID(N'[dbo].[spExplanationSelPgd]') IS NOT NULL
	DROP PROCEDURE [dbo].[spExplanationSelPgd]

IF OBJECT_ID(N'[dbo].[spExplanationSelByExplanationText]') IS NOT NULL
	DROP PROCEDURE [dbo].[spExplanationSelByExplanationText]

IF OBJECT_ID(N'[dbo].[spExplanationSelByExplanationID]') IS NOT NULL
	DROP PROCEDURE [dbo].[spExplanationSelByExplanationID]

IF OBJECT_ID(N'[dbo].[spExplanationDelByExplanationText]') IS NOT NULL
	DROP PROCEDURE [dbo].[spExplanationDelByExplanationText]

IF OBJECT_ID(N'[dbo].[spExplanationDelByExplanationID]') IS NOT NULL
	DROP PROCEDURE [dbo].[spExplanationDelByExplanationID]

--endregion

GO

--region [dbo].[spExplanationIns]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   mbravo using CodeSmith 5.0.0.0
-- Template:       mbStoredProcedures.cst
-- Procedure Name: [dbo].[spExplanationIns]
-- Date Generated: Thursday, September 23, 2010
------------------------------------------------------------------------------------------------------------------------


CREATE PROCEDURE [dbo].[spExplanationIns]
	@ExplanationID int,
	@ExplanationText nvarchar(500)
AS

SET NOCOUNT ON

INSERT INTO [dbo].[Explanations] (
[ExplanationText]
) OUTPUT INSERTED.ExplanationID AS ExplanationID

VALUES (
@ExplanationText
)
;



--endregion

GO

--region [dbo].[spExplanationUpd]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   mbravo using CodeSmith 5.0.0.0
-- Template:       mbStoredProcedures.cst
-- Procedure Name: [dbo].[spExplanationUpd]
-- Date Generated: Thursday, September 23, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[spExplanationUpd]
	@ExplanationID int,
	@ExplanationText nvarchar(500)
AS

SET NOCOUNT ON

UPDATE [dbo].[Explanations] SET
	[ExplanationText] = @ExplanationText
WHERE
		[ExplanationID] = @ExplanationID
;
SELECT	@ExplanationID as ExplanationID;



--endregion

GO

--region [dbo].[spExplanationInsUpd]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   mbravo using CodeSmith 5.0.0.0
-- Template:       mbStoredProcedures.cst
-- Procedure Name: [dbo].[spExplanationInsUpd]
-- Date Generated: Thursday, September 23, 2010
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[spExplanationInsUpd]
	@ExplanationID int,
	@ExplanationText nvarchar(500)
AS

SET NOCOUNT ON

IF EXISTS(SELECT [ExplanationID] FROM [dbo].[Explanations] WHERE [ExplanationID] = @ExplanationID)
BEGIN
	UPDATE [dbo].[Explanations] SET
		[ExplanationText] = @ExplanationText
	WHERE
		[ExplanationID] = @ExplanationID
	SELECT	@ExplanationID as ExplanationID;	
END
ELSE
BEGIN
	INSERT INTO [dbo].[Explanations] (
		[ExplanationText]
	) 
	OUTPUT INSERTED.ExplanationID AS ExplanationID
	VALUES (
		@ExplanationText
	)
	;
END


--endregion

GO

--region [dbo].[spExplanationDel]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   mbravo using CodeSmith 5.0.0.0
-- Template:       mbStoredProcedures.cst
-- Procedure Name: [dbo].[spExplanationDel]
-- Date Generated: Thursday, September 23, 2010
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[spExplanationDel]
	@ExplanationID int
AS
SET NOCOUNT ON
DELETE FROM [dbo].[Explanations]
WHERE
	[ExplanationID] = @ExplanationID
--endregion

GO


--region [dbo].[spExplanationDelByExplanationText]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   mbravo using CodeSmith 5.0.0.0
-- Template:       mbStoredProcedures.cst
-- Procedure Name: [dbo].[spExplanationDelByExplanationText]
-- Date Generated: Thursday, September 23, 2010
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[spExplanationDelByExplanationText]
	@ExplanationText nvarchar(500)
AS

SET NOCOUNT ON

DELETE FROM [dbo].[Explanations]
WHERE
	[ExplanationText] = @ExplanationText

--endregion

GO

--region [dbo].[spExplanationDelByExplanationID]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   mbravo using CodeSmith 5.0.0.0
-- Template:       mbStoredProcedures.cst
-- Procedure Name: [dbo].[spExplanationDelByExplanationID]
-- Date Generated: Thursday, September 23, 2010
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[spExplanationDelByExplanationID]
	@ExplanationID int
AS

SET NOCOUNT ON

DELETE FROM [dbo].[Explanations]
WHERE
	[ExplanationID] = @ExplanationID

--endregion

GO

--region [dbo].[spExplanationDelDyn]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   mbravo using CodeSmith 5.0.0.0
-- Template:       mbStoredProcedures.cst
-- Procedure Name: [dbo].[spExplanationDelDyn]
-- Date Generated: Thursday, September 23, 2010
------------------------------------------------------------------------------------------------------------------------


CREATE PROCEDURE [dbo].[spExplanationDelDyn]
	@WhereCondition nvarchar(500)
AS

SET NOCOUNT ON

DECLARE @SQL nvarchar(3250)

SET @SQL = '
DELETE FROM
	[dbo].[Explanations]
WHERE
	' + @WhereCondition

EXEC sp_executesql @SQL

--endregion

GO

--region [dbo].[spExplanationSel]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   mbravo using CodeSmith 5.0.0.0
-- Template:       mbStoredProcedures.cst
-- Procedure Name: [dbo].[spExplanationSel]
-- Date Generated: Thursday, September 23, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[spExplanationSel]
	@ExplanationID int
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

SELECT
	[ExplanationID],
	[ExplanationText]
FROM
	[dbo].[Explanations]
WHERE
	[ExplanationID] = @ExplanationID

--endregion

GO

--region [dbo].[spExplanationSelByExplanationText]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   mbravo using CodeSmith 5.0.0.0
-- Template:       mbStoredProcedures.cst
-- Procedure Name: [dbo].[spExplanationSelByExplanationText]
-- Date Generated: Thursday, September 23, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[spExplanationSelByExplanationText]
	@ExplanationText nvarchar(500)
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

SELECT
	[ExplanationID],
	[ExplanationText]
FROM
	[dbo].[Explanations]
WHERE
	[ExplanationText] = @ExplanationText

--endregion

GO

--region [dbo].[spExplanationSelByExplanationID]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   mbravo using CodeSmith 5.0.0.0
-- Template:       mbStoredProcedures.cst
-- Procedure Name: [dbo].[spExplanationSelByExplanationID]
-- Date Generated: Thursday, September 23, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[spExplanationSelByExplanationID]
	@ExplanationID int
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

SELECT
	[ExplanationID],
	[ExplanationText]
FROM
	[dbo].[Explanations]
WHERE
	[ExplanationID] = @ExplanationID

--endregion

GO

--region [dbo].[spExplanationSelDyn]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   mbravo using CodeSmith 5.0.0.0
-- Template:       mbStoredProcedures.cst
-- Procedure Name: [dbo].[spExplanationSelDyn]
-- Date Generated: Thursday, September 23, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[spExplanationSelDyn]
	@WhereCondition nvarchar(500),
	@OrderByExpression nvarchar(250) = NULL
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

DECLARE @SQL nvarchar(3250)

SET @SQL = '
SELECT
	[ExplanationID],
	[ExplanationText]
FROM
	[dbo].[Explanations]
'
IF @WhereCondition IS NOT NULL AND LEN(@WhereCondition) > 0
BEGIN
	SET @SQL = @SQL + '
WHERE
	' + @WhereCondition
END

IF @OrderByExpression IS NOT NULL AND LEN(@OrderByExpression) > 0
BEGIN
	SET @SQL = @SQL + '
ORDER BY
	' + @OrderByExpression
END

EXEC sp_executesql @SQL

--endregion

GO

--region [dbo].[spExplanationSelAll]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   mbravo using CodeSmith 5.0.0.0
-- Template:       mbStoredProcedures.cst
-- Procedure Name: [dbo].[spExplanationSelAll]
-- Date Generated: Thursday, September 23, 2010
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[spExplanationSelAll]
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

SELECT
	[ExplanationID],
	[ExplanationText]
FROM
	[dbo].[Explanations]
	


--endregion

GO

--region [dbo].[spExplanationSelPgd]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   mbravo using CodeSmith 5.0.0.0
-- Template:       mbStoredProcedures.cst
-- Procedure Name: [dbo].[spExplanationSelPgd]
-- Date Generated: Thursday, September 23, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[spExplanationSelPgd]
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

SELECT
	[ExplanationID],
	[ExplanationText]
FROM
	[dbo].[Explanations]

--endregion

GO

